#!/bin/bash
# description: Modify enp2s0 device name to eth1
# author: ZhangChiQian<zhangchiqian@uniontech.com>

echo "开始修改脚本，请以root身份运行该脚本"
ifname=enp6s0   #修改前网卡名称
to_ifname=eth1  #修改后网卡名称
hwaddr=$( ip link show $ifname | awk '(NR==2){print $2}')

systemctl start NetworkManager
systemctl enable NetworkManager
#删掉原有的网卡配置文件
nmcli c delete $(nmcli c | awk '$4==''"'$ifname'"''{print $1}') &> /dev/null
nmcli c delete ${ifname}  &> /dev/null
nmcli c delete ${to_ifname}  &> /dev/null
if [ -f /etc/sysconfig/network-scripts/ifcfg-${to_ifname} ];then
    rm -f  /etc/sysconfig/network-scripts/ifcfg-${to_ifname}
fi
#用nmcli 增加一个连接到指定网卡
nmcli connection add type ethernet con-name "$to_ifname"  ifname "$ifname" connection.autoconnect yes
nmcli connection modify "$to_ifname" 802-3-ethernet.mac-address $hwaddr
nmcli connection modify "$to_ifname" connection.interface-name "$to_ifname"
sed -i 's@net.ifnames=0@@g' /etc/default/grub
sed -i 's@biosdevname=0@@g' /etc/default/grub
sed -i 's@rhgb@net.ifnames=0 biosdevname=0 &@' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
